package com.tenglong.teacher.ui.utils;



import com.netease.nimlib.p.a;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.tenglong.teacher.data.resultInfo.AnswerInfo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class NimUtils {
    public static int ANSWER_QESTION_TYPE = 0;
    public static int HOMEWORK_TYPE = 1;
    public static int ADD_CLASS_TYPE = 2;
    public static int WRONG_TYPE = -1;

    public static IMMessage createSessionIdUtiMesssage(int type, String sessionId) {
        IMMessage msg = MessageBuilder.createTextMessage("null", SessionTypeEnum.P2P, "NULL");
        return addSessionId(msg, type, sessionId);

    }

    public static IMMessage createQuestionSessionIdUtiMesssage(AnswerInfo item, int type, String sessionId) {
        IMMessage msg = MessageBuilder.createTextMessage("null", SessionTypeEnum.P2P, "NULL");
        msg.setDirect(MsgDirectionEnum.In);
        msg.setStatus(MsgStatusEnum.read);
        if (msg instanceof a) {
            ((a) msg).a(0);
            ((a) msg).a(SessionTypeEnum.P2P);
            ((a) msg).b(item.createDate);
            msg.setFromAccount(item.studentimid);
        }
        return addSessionIdWithOher(msg, type, sessionId, item);
    }


    public static IMMessage addSessionIdWithOher(IMMessage msg, int type, String sessionId, AnswerInfo object) {
        Map<String, Object> extSessionId = new HashMap<>(5);
        extSessionId.put("sessionId", sessionId);
        extSessionId.put("type", Integer.valueOf(type));
        extSessionId.put("zuiwenuri", object.questioning);
        extSessionId.put("zuiwenimguri", object.firstpicmsg);
        extSessionId.put("zuiwentxt", object.firsttextmsg);
        msg.setRemoteExtension(extSessionId);
        return msg;

    }

    public static IMMessage addSessionId(IMMessage msg, int type, String sessionId) {
        Map<String, Object> extSessionId = new HashMap<>(2);
        extSessionId.put("sessionId", sessionId);
        extSessionId.put("type", Integer.valueOf(type));
        msg.setRemoteExtension(extSessionId);
        return msg;

    }

    public static int getMsgType(IMMessage msg) {
        Map<String, Object> ext = msg.getRemoteExtension();
        if (ext != null) {
            return ((Integer) ext.get("type")).intValue();
        } else {
            return WRONG_TYPE;
        }

    }

    public static String getMsgSessionId(IMMessage msg) {
        Map<String, Object> ext = msg.getRemoteExtension();
        if (ext != null) {
            return ext.get("sessionId").toString();
        } else {
            return null;
        }

    }

    public static boolean shouldDropIncomingMsg(IMMessage msg, int type, String sessionId) {
        Map<String, Object> ext = msg.getRemoteExtension();
        if (ext == null) {
            return true;
        }
        String sessionIdMsg = ext.get("sessionId").toString();
        int typeMsg = ((Integer) ext.get("type")).intValue();
        if (ext != null) {
            if (sessionId != null && type != WRONG_TYPE && sessionId.equals(sessionIdMsg) && typeMsg == type) {
                return false;
            } else {
                return true;
            }

        } else {
            return true;
        }

    }

    public static List<IMMessage> dropIncomingMsg(List<IMMessage> msgs, int type, String sessionId) {
        Iterator<IMMessage> iterator = msgs.iterator();
        while (iterator.hasNext()) {
            IMMessage ims = iterator.next();
            if (shouldDropIncomingMsg(ims, type, sessionId)) {
                iterator.remove();
            }

        }
        return msgs;

    }

    public static List<IMMessage> fetchMessagesWithSessionId(List<IMMessage> messages, String sessionId) {
        Iterator<IMMessage> iterator = messages.iterator();
        while (iterator.hasNext()) {
            IMMessage ims = iterator.next();
            Map<String, Object> ext = ims.getRemoteExtension();
            if (ext == null) {
                iterator.remove();
            }
            if (ext != null && !ext.isEmpty()) {
                if (ext.get("sessionId") == null || !ext.get("sessionId").equals(sessionId)) {
                    iterator.remove();
                }
            }
            //do something
        }
        return messages;

    }
}



